import React from 'react';
import {connect} from 'dva'

import { BasicAbstract , CommAbstract } from 'components/Abstract'
import Title from 'components/Title'
import moment from "moment/moment";

import { config } from 'utils'
const { scada } = config;
const {
  Token,DType,StationIds
} = scada;
const {
  source,supply,drug
} = StationIds;

const nowTimeStamp = Date.now();
const now = new Date(nowTimeStamp);
const time = moment(now).format('YYYY')

@connect(({production}) => ({...production}))
class Indicators extends React.Component {
    static defaultProps = {
      abstractConf: {
        label: '重点指标监控', list: [
          { label: '源水量', field: 'source', options:[{vField: 'month', tField: '本月总量'},{vField: 'year', tField: '本年总量'}], unit: 'm3', icon: 'water' },
          { label: '供水量', field: 'supply',options:[{vField: 'month', tField: '本月总量'},{vField: 'year', tField: '本年总量'}], unit: 'm3', icon: 'water1' },
          { label: '千吨水电耗', field: 'electric',options:[{vField: 'month', tField: '本月值'},{vField: 'year', tField: '本年均值'}], unit: 'kw·h/km³', icon: 'ktd' },
          { label: '千吨水药耗', field: 'drug',options:[{vField: 'month', tField: '本月值'},{vField: 'year', tField: '本年均值'}], unit: 'kg/km³', icon: 'kty' },
          { label: '盐酸消耗', field: 'HCL',options:[{vField: 'month', tField: '本月值'},{vField: 'year', tField: '本年均值'}], unit: 'kg', icon: 'HCl' },
          { label: '氯酸钠消耗', field: 'NaClO3',options:[{vField: 'month', tField: '本月值'},{vField: 'year', tField: '本年均值'}], unit: 'kg', icon: 'NaClO3' },
        ]
      },
    }

  componentDidMount() {
    this.init();
  }

  init = async () => {
    await this.setDateIndicators()
  }
  // 获取指标
  setDateIndicators = async () => {
    /*源水量
    */
    source.map((item, idx) => {
      let server = {type: 'production/querySource',payload: { Token: Token,DTypes:["SJLJ"], Interval: 86400, Begin: moment(`${time}-01-01 00:00:00`).unix(),
          End: moment(`${time}-12-31 23:59:59`).unix(),StationId: item,type: idx} };
      this.querySJLJIndicators(server);
    })
    /*供水量
     */
    let serverS = {type: 'production/querySupply',payload: { Token: Token,DTypes:["SJLJ"], Interval: 86400,
        Begin: moment(`${time}-01-01 00:00:00`).unix(),End: moment(`${time}-12-31 23:59:59`).unix(),StationId: supply[0],type: 1 } };
    this.querySJLJIndicators(serverS);
    /*药耗
     */
    let serverD = {type: 'production/queryDrug',payload:{payload: { where: `DATATIME>='${time}0101' and DATATIME<='${time}1231'` },tablename: 'zddrugmanage',type: 1}}
    this.querySJLJIndicators(serverD);
    /*电耗
     */
    let serverE = {type: 'production/queryElectric',payload:{payload: { where: `DATATIME>='${time}0101' and DATATIME<='${time}1231'` },tablename: 'zdelectricmanage',type: 1}}
    this.querySJLJIndicators(serverE);
  }

  querySJLJIndicators = async (server) => {
    await this.props.dispatch(server);
  }

  render() {
    const { label, list } = this.props.abstractConf;;
    const startDayTimestamp = moment(moment(now).startOf('day')).unix();
    const startWeekTimestamp = moment(moment(now).startOf('week')).unix();
    const startMonthTimestamp = moment(moment(now).startOf('month')).unix();
    const startYearTimestamp = moment(moment(now).startOf('year')).unix();
    let sourceDaySum = 0;
    let sourceWeekSum = 0;
    let sourceMonthSum = 0;
    let sourceYearSum = 0;
    source.map((item, idx) => {
      const resouseVals = this.props[`SourceResource${idx}`] && this.props[`SourceResource${idx}`][0] && this.props[`SourceResource${idx}`][0].Vals
        ? this.props[`SourceResource${idx}`][0].Vals : [];
      resouseVals.map(term => {
        const {Time, Report} = term;
        const {maxv} = Report;
        if (Time >= startDayTimestamp) {
          sourceDaySum += maxv
        }
        if (Time >= startWeekTimestamp) {
          sourceWeekSum += maxv
        }
        if (Time >= startMonthTimestamp) {
          sourceMonthSum += maxv
        }
        if (Time >= startYearTimestamp) {
          sourceYearSum += maxv
        }
      })
    })
    let SupplyDaySum = 0;
    let SupplyWeekSum = 0;
    let SupplyMonthSum = 0;
    let SupplyYearSum = 0;
    const supplyVals = this.props["SupplyResource1"] && this.props["SupplyResource1"][0] && this.props["SupplyResource1"][0].Vals
      ? this.props["SupplyResource1"][0].Vals : [];
    supplyVals.map(term => {
        const { Time,Report } = term;
        const { maxv } = Report;
        if (Time >= startDayTimestamp) {
          SupplyDaySum += maxv
        }
        if(Time >=  startWeekTimestamp){
          SupplyWeekSum += maxv
        }
        if(Time >=  startMonthTimestamp){
          SupplyMonthSum += maxv
        }
        if(Time >=  startYearTimestamp){
          SupplyYearSum += maxv
        }
      }
    )
    let drugWeekSum = 0;let drugMonthSum = 0;let drugYearSum = 0;
    let hClWeekSum = 0;let hClMonthSum = 0;let hClYearSum = 0;
    let naClO3WeekSum = 0;let naClO3MonthSum = 0;let naClO3YearSum = 0;
    const drugVals = this.props["DrugResource1"] ? this.props["DrugResource1"] : [];
    drugVals.map(ele => {
      const { attributes } = ele;
      let { DATATIME,NAClO3,HYDROCHLORIC_ACID } = attributes;
      NAClO3 = parseFloat(NAClO3);
      HYDROCHLORIC_ACID = parseFloat(HYDROCHLORIC_ACID);
      const Time =  moment(DATATIME, "YYYY-MM-DD").unix();
      if(Time >=  startWeekTimestamp){
        drugWeekSum += NAClO3;
        drugWeekSum += HYDROCHLORIC_ACID;
        naClO3WeekSum += NAClO3;
        hClWeekSum += HYDROCHLORIC_ACID;
      }
      if(Time >=  startMonthTimestamp){
        drugMonthSum += NAClO3;
        drugMonthSum += HYDROCHLORIC_ACID;
        naClO3MonthSum += NAClO3;
        hClMonthSum += HYDROCHLORIC_ACID;
      }
      if(Time >=  startYearTimestamp){
        drugYearSum += NAClO3;
        drugYearSum += HYDROCHLORIC_ACID;
        naClO3YearSum += NAClO3;
        hClYearSum += HYDROCHLORIC_ACID;
      }
    })
    let electricWeekSum = 0;let electricMonthSum = 0;let electricYearSum = 0;
    const electricVals = this.props["ElectricResource1"] ? this.props["ElectricResource1"] : [];
    electricVals.map(ele => {
      const { attributes } = ele;
      let { DATATIME,ELECTRICITY_CONSUMPTION} = attributes;
      ELECTRICITY_CONSUMPTION = parseFloat(ELECTRICITY_CONSUMPTION);
      const Time =  moment(DATATIME, "YYYY-MM-DD").unix();
      if(Time >=  startWeekTimestamp){
        electricWeekSum += ELECTRICITY_CONSUMPTION;
      }
      if(Time >=  startMonthTimestamp){
        electricMonthSum += ELECTRICITY_CONSUMPTION;
      }
      if(Time >=  startYearTimestamp){
        electricYearSum += ELECTRICITY_CONSUMPTION;
      }
    })
    const months = moment(now).month()+1;
    const dataSource = {
      "source":{"total":sourceDaySum,"month":sourceMonthSum,"year":sourceYearSum},
      "supply":{"total":SupplyDaySum,"month":SupplyMonthSum,"year":SupplyYearSum},
      "drug":{"total":SupplyWeekSum>0?(drugWeekSum*1000.0/SupplyWeekSum).toFixed(2):0,
        "month":SupplyMonthSum>0?(drugMonthSum*1000.0/SupplyMonthSum).toFixed(2):0,
        "year":SupplyYearSum>0?(drugYearSum*1000.0/SupplyYearSum).toFixed(2):0},
      "electric":{"total":SupplyWeekSum>0?(electricWeekSum*1000.0/SupplyWeekSum).toFixed(2):0,
        "month":SupplyMonthSum>0?(electricMonthSum*1000.0/SupplyMonthSum).toFixed(2):0,
        "year":SupplyYearSum>0?(electricYearSum*1000.0/SupplyYearSum).toFixed(2):0},
      "HCL":{"total":hClWeekSum,"month":hClMonthSum,"year":(hClYearSum/months).toFixed(2)},
      "NaClO3":{"total":naClO3WeekSum,"month":naClO3MonthSum,"year":(naClO3YearSum/months).toFixed(2)},
    }
    return (
      <Title title={label}  style={{'padding': '2%'}}>
        <CommAbstract
          columns={list}
          dataSource={dataSource}
          type={'3'}
          isColorful={true}
          className={'col-12'}/>
      </Title>
    )
  }
}

export default Indicators;
